package com.vision.admin.company.cl.ui;

import static com.vision.admin.cl.AdminServices.companySvc;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.TextArea;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.vision.admin.cl.resource.AdminResources;
import com.vision.core.cl.BaseModule;
import com.vision.core.cl.Callback;
import com.vision.core.cl.event.CompanyCreatedEvent;
import com.vision.core.cl.ui.UI;
import com.vision.core.cl.util.FieldUtils;
import com.vision.core.cm.db.data.Company;

/**
 * 
 * @author Mark
 *
 */
public class CreateCompanyWindow implements IsWidget, Editor<Company> {
	
	interface Binder extends UiBinder<Widget, CreateCompanyWindow> { }
	interface CompanyDriver extends SimpleBeanEditorDriver<Company, CreateCompanyWindow> { }
	
	private static final Binder binder = GWT.create(Binder.class);
	
	@UiField VerticalLayoutContainer formContainer;
	
	@UiField TextField code;
	@UiField TextField name;
	@UiField TextArea address;
	
	@Ignore @UiField TextButton saveBtn;
	@Ignore @UiField TextButton cancelBtn;

	@Ignore @UiField Window window;
	
	private CompanyDriver driver = GWT.create(CompanyDriver.class); // Used to bind the grid to the form editor
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	public Widget asWidget() {
		window = (Window) binder.createAndBindUi(this);
		window.getHeader().setIcon(AdminResources.INSTANCE.companies());
		
		// Add validators to fields
		FieldUtils.minMaxOf(code, 1, Company.FieldSpecs.CODE_MAX);
		FieldUtils.minMaxOf(name, 1, Company.FieldSpecs.NAME_MAX);
		FieldUtils.maxOf(address, Company.FieldSpecs.ADDRESS_MAX);
		
		driver.initialize(this);
		driver.edit(new Company());
		
		return window;
	}
	
	// UiFactory Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	// Logic %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@UiHandler("saveBtn")
	void saveClicked(SelectEvent e) {
		Company company = driver.flush();
		if (driver.hasErrors() || company == null)
			return;
		
		UI.mask(window, "Creating company...");
		companySvc().create(company, new Callback<Company>(window) {
			@Override
			protected void handleSuccess(Company result) {
				window.hide(saveBtn);
				BaseModule.fireEvent(new CompanyCreatedEvent(result));
			}
		});
	}
	
	@UiHandler("cancelBtn")
	void cancelClicked(SelectEvent e) {
		window.hide(cancelBtn);
	}
	
	// Helper Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

}
